package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.PrintPhonesData;
import com.example.utils.SortedListOf;

public class PrintPhonesHelper extends WebDriverHelperBase
{
	public PrintPhonesHelper(ApplicationManager manager)
	{
		super(manager);
	}

	public SortedListOf<PrintPhonesData> getContactsFromPrintPhones()
	{
		SortedListOf<PrintPhonesData> contacts = new SortedListOf<PrintPhonesData>();

		manager.navigateTo().printPhonesPage();

		List<WebElement> cards = driver.findElements(By.tagName("td"));
		for (WebElement card : cards)
		{
			String phone = card.getText();

			PrintPhonesData contact = new PrintPhonesData()
					.withFullName(card.findElement(By.tagName("b")).getText())
					.withPhoneHome(phone.substring(phone.indexOf("H: ") + 3, phone.indexOf("M: ") - 1));

			contacts.add(contact);
		}
		return contacts;
	}

	public SortedListOf<PrintPhonesData> getContactsFromMainPage()
	{
		SortedListOf<PrintPhonesData> contacts = new SortedListOf<PrintPhonesData>();

		manager.navigateTo().mainPage();
		List<WebElement> lines = driver.findElements(By.name("entry"));
		for (WebElement line : lines)
		{
			List<WebElement> cells = line.findElements(By.tagName("td"));

			PrintPhonesData contact = new PrintPhonesData()
					.withFullName(cells.get(2).getText() + " " + cells.get(1).getText())
					.withPhoneHome(cells.get(4).getText());

			contacts.add(contact);
		}
		return contacts;
	}

}
